package com.lswms.job;

import com.lswms.common.utils.ZqDateUtil;
import com.lswms.dto.YnLotteryDTO;
import com.lswms.dto.YnSignDTO;
import com.lswms.entity.YnLotteryEntity;
import com.lswms.entity.YnSignEntity;
import com.lswms.service.YnLotteryService;
import com.lswms.service.YnSignService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 
 * @author Administrator
 *
 */
@Component
@Slf4j
public class RobotTasks {

	@Autowired
	private YnSignService ynSignService;
	
	@Autowired
	private YnLotteryService ynLotteryService;
	
    /**
     * 每天5点之后添加抽奖人数（机器人数据）（10秒一次）
     */
    @Scheduled(cron="*/10 * * * * ?")
    public void setRobotLottery(){
    	log.info("开始每天5点之后添加抽奖人数（机器人数据）***");
        //添加新一期的挑战期数
    	String yyyyMMdd = ZqDateUtil.yyyyMMdd(ZqDateUtil.getNow());
    	String yyyy_MM_dd = ZqDateUtil.yyyy_MM_dd(ZqDateUtil.getNow());
    	
    	Date now = ZqDateUtil.getNow();
    	if(now.compareTo(ZqDateUtil.getDatehms(yyyy_MM_dd+" 05:00:00")) >= 0 && 
    			now.compareTo(ZqDateUtil.getDatehms(yyyy_MM_dd+" 23:59:59")) <=0){
        	//查询当前期数抽奖信息
    		Map<String,Object> map = new HashMap<String,Object>();
    		map.put("signNo", yyyyMMdd);
    		List<YnLotteryDTO> list = ynLotteryService.list(map);
    		YnLotteryDTO dto = new YnLotteryDTO();
    		if(list.size() > 0){
    			dto = list.get(0);
    		}
    		//机器人上限
    		if(dto.getLotteryCountj() < 19000){
        		Random random = new Random();
        		int temp = random.nextInt(10);
        		//更新机器人数据
        		YnLotteryEntity entity = new YnLotteryEntity();
        		entity.setId(dto.getId());
        		entity.setLotteryCountj(dto.getLotteryCountj() + temp);
        		entity.setUpdateDate(ZqDateUtil.getNow());
        		ynLotteryService.updateById(entity);
    		}
    	}
    	log.info("结束每天5点之后添加抽奖人数（机器人数据）***");
    }
    /**
     * 每天0点之后添加挑战人数（机器人数据）（1分一次）
     */
    @Scheduled(cron="0 */1 * * * ?")
    public void setRobotSign(){
    	log.info("开始每天0点之后添加挑战人数（机器人数据）***");
        //添加新一期的挑战期数
    	String yyyyMMdd = ZqDateUtil.yyyyMMdd(ZqDateUtil.getNow());
    	//期数信息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("signNo", yyyyMMdd);
    	List<YnSignDTO> signList = ynSignService.list(map);
    	for(YnSignDTO dto : signList){
    		//机器人上限
    		if(dto.getSignCountj() < 2400){
        		Random random = new Random();
        		int temp = random.nextInt(10);
        		BigDecimal amount = dto.getAmount().multiply(new BigDecimal(temp));
        		BigDecimal total = dto.getRewardj().add(amount);
        		//更新机器人数据
        		YnSignEntity entity = new YnSignEntity();
        		entity.setId(dto.getId());
        		entity.setSignCountj(dto.getSignCountj() + temp);
        		entity.setRewardj(total);
        		entity.setUpdateDate(ZqDateUtil.getNow());
        		ynSignService.updateById(entity);
    		}
    	}
    	log.info("结束每天0点之后添加挑战人数（机器人数据）***");
    }
}
